
package com.liao.badminton.controller;

import cn.hutool.core.util.ObjectUtil;
import com.liao.badminton.code.*;
import com.liao.badminton.code.myenum.RoleCodeEnum;
import com.liao.badminton.code.util.JwtTokenUtils;
import com.liao.badminton.dto.SystemUsersDto;
import com.liao.badminton.entity.SystemUsers;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.service.SystemRoleService;
import com.liao.badminton.service.SystemUsersService;
import com.liao.badminton.vo.SystemUsersVo;
import org.apache.http.HttpResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.liao.badminton.code.validation.Delete;
import com.liao.badminton.code.validation.Insert;
import com.liao.badminton.code.validation.Update;
import com.liao.badminton.code.validation.FindDetail;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liaocheng
 * @date 2024-11-01
 * description:用户信息表
 */
@RestController
@RequestMapping(value = "/systemUsers")
// @PreAuthorize("@ss.hasPermission('systemUsers')")//权限管理;
public class SystemUsersController {

    //  private static  Logger logger= LoggerFactory.getLogger(SystemUsersController.class);

    @Autowired
    private SystemUsersService systemUsersService;

    @Resource
    private SystemRoleService systemRoleService;

    /**
     * 用户注册
     */
    @RequestMapping(value = "/register")
    // @RequiresPermissions("systemUsers:add")//权限管理;
    public ResponseRest register(@RequestBody @Validated(Insert.class) SystemUsersDto systemUsers, HttpServletResponse response) throws ProjectException {
        SystemUsersVo user = systemUsersService.register(systemUsers, response);
        return ResponseRest.suc("register-注册成功！", user);
    }

    /**
     * 手机号登录
     */
    @RequestMapping(value = "/loginByPhone")
    // @RequiresPermissions("systemUsers:add")//权限管理;
    public ResponseRest loginByPhone(@RequestParam("phone") String phone, @RequestParam("password") String password, HttpServletResponse response) throws ProjectException {
        SystemUsersVo user = systemUsersService.loginByPhone(phone, password, response);

        return ResponseRest.suc("loginByPhone-登录成功！", user);
    }

    /**
     * 新增systemUsers
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("@ss.hasPermission('systemUsers:add')")//权限管理;
    public ResponseRest<Long> add(@RequestBody @Validated(Insert.class) SystemUsersDto systemUsers) throws ProjectException {
        return systemUsersService.saveSystemUsers(systemUsers);
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deletes")
    @PreAuthorize("@ss.hasPermission('systemUsers:deletes')")//权限管理;
    public ResponseRest<Boolean> deletes(@RequestBody @Validated(Delete.class) SystemUsersDto param) throws ProjectException {
        return systemUsersService.deletesSystemUsers(param);
    }

    /**
     * 修改systemUsers
     */
    @RequestMapping(value = "/update")
    @PreAuthorize("@ss.hasPermission('systemUsers:update')")//权限管理;
    public ResponseRest<Boolean> update(@RequestBody @Validated(Update.class) SystemUsersDto param) throws ProjectException {
        return systemUsersService.updateSystemUsers(param.invertEntity());
    }

    @RequestMapping(value = "/findDetail")
    @PreAuthorize("@ss.hasPermission('SystemUsers:findDetail')")//权限管理;
    public ResponseRest findDetail(@RequestBody @Validated(FindDetail.class) SystemUsersDto param) throws ProjectException {
        return ResponseRest.suc("findDetail",systemUsersService.findDetail(param));
    }

    /**
     * 分页查询列表
     */
    @RequestMapping(value = "/list")
    @PreAuthorize("@ss.hasPermission('systemUsers:list')")//权限管理;
    public ResponseRest list(@RequestBody PageParam<SystemUsersDto> body) throws ProjectException {
        return systemUsersService.findSystemUsersByPageParam(body);
    }

}
